/** 
 * 自定义JS
 */


var domain = '/api/admin/'; //接口前缀
var cacheLogin = 'login'; //登录缓存


// 列表数据分页
function getPageHtml(e){
    // <div class="dataTables_info" style="float:left;">
    // 	显示 1 到 2 ，共 2 条
    // </div>
    // <div class="dataTables_paginate paging_simple_numbers" style="float:right;"><a class="paginate_button previous disabled" aria-controls="DataTables_Table_0" data-dt-idx="0" tabindex="0" id="DataTables_Table_0_previous">上一页</a><span><a class="paginate_button current" aria-controls="DataTables_Table_0" data-dt-idx="1" tabindex="0">1</a></span><a class="paginate_button next disabled" aria-controls="DataTables_Table_0" data-dt-idx="2" tabindex="0" id="DataTables_Table_0_next">下一页</a></div>
    var start = parseInt(e.start), limit = parseInt(e.limit), total = e.total, ps = 0, prefixUrl = e.prefixUrl;
    //console.log(e,start,limit,total);
    var h = '';
    h += '<div style="margin:10px;">';
    h += '<div class="dataTables_info" style="float:left;">';
    h += '当前第 '+start+' 页，共 '+Math.ceil(total/limit)+' 页，共 '+total+' 条，每页 '+limit+' 条';
    h += '</div>';
    h += '<div class="dataTables_paginate paging_simple_numbers" style="float:right;">';
    var pre = '<a href="javascript:;" style="padding:10px;color:#888;" onclick="pop({msg:\'没有了\'})">上一页</a>';
    if (start>1) {
        //ps = (start * 1 - 1) * limit;
        ps = (start * 1 - 1);
        pre = '<a href="javascript:;" style="padding:10px;color:#0088CC;" onclick="window.location.href=\'/admin/'+prefixUrl+'.html?pstart='+ps+'&plimit='+limit+'\';">上一页</a>';
    }
    h += pre;
    var next = '<a href="javascript:;" style="padding:10px;color:#888;" onclick="pop({msg:\'没有了\'})">下一页</a>';
    if (start*limit<total) {
        //ps = (start * 1 + 1) * limit;
        ps = (start * 1 + 1);
        next = '<a href="javascript:;" style="padding:10px;color:#0088CC;" onclick="window.location.href=\'/admin/'+prefixUrl+'.html?pstart='+ps+'&plimit='+limit+'\';">下一页</a>';
    }
    h += next;
    h += '</div>';
    h += '</div>';
    $('#page').html(h);
}


//获取登录信息
function getLoginInfo(){
    return getCache(cacheLogin);
}


//设置登录信息
function setLoginInfo(e){
    return setCache({key:cacheLogin,val:e.val});
}


//退出登录
function setLogout(){
    return delCache({key:cacheLogin});
}


//获取地址栏中参数
function getParamByUrl(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r==null) return '';
     return  unescape(r[2]); 
}


//显示指定字符数
function getStrByNum(e){
    var str = e.str, num = e.num;
    if (str.length<num) {
        return str;
    } else {
        return str.substring(0,num-1)+'...';
    }
}


//自定义弹窗
function pop(e){
    var msg = e.msg, t = e.t;
    t = t ? t*1000 : 1000;
    var h = '';
    h += '<div style="position:fixed;top:0;left:0;z-index:10000;">';
    h += '<div style="width:100%;height:100%;min-height:500px;background:#000;opacity:0.2;position:fixed;top:0;left:0;z-index:10000;"></div>';
    h += '<div style="padding:10px 20px;min-width:100px;max-width:300px;text-align:center;background:#000;color:#FFF;border-radius:5px;position:fixed;bottom:40%;left:40%;z-index:10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$("#pop").html("");},'+t+')</script>';
    $('#pop').html(h);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//删除缓存
function clearCache(){
    window.localStorage.clear();
}